package top.vanchor.myblog2back.controller.user;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.entity.BlogReport;
import top.vanchor.myblog2back.service.IBlogReportService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-02-18
 */
@Api("举报控制器")
@RestController
@RequestMapping("/blogReport")
public class BlogReportController {

    @Resource
    private IBlogReportService service;

    @ApiOperation("提交举报")
    @PostMapping("/toReport")
    @ResponseBody
    public JSONObject toReport(@RequestBody BlogReport bean){
        JSONObject jsonObject = new JSONObject();
        try {
            if (service.isUserReported(bean) > 0){
                //这个人举报过了，就不让再举报了
                jsonObject.put("code",430);
                jsonObject.put("msg","您的举报已发送申请，请勿重复举报");
                return jsonObject;
            }
            bean.setReportHandleTime(String.valueOf(System.currentTimeMillis()/1000));
            bean.setReportStatus(0);
            if (service.toReport(bean) > 0){
                jsonObject.put("code",200);
                jsonObject.put("msg","举报成功");
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","服务器异常");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常");
        }
        return jsonObject;
    }
}
